// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// m [author] rhyme_lph
// Z [email]  rhymelph@gmail.com
// U [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/contact_person_entity.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/contact_person_item.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/contact/detail/contact_detail_person_provider.dart';
import 'package:finance_app/src/provider/dashboard/contact/business_contact_detail_provider.dart';

class ContactDetailPersonView extends StatefulWidget {
  @override
  _ContactDetailPersonViewState createState() =>
      _ContactDetailPersonViewState();
}

class _ContactDetailPersonViewState extends State<ContactDetailPersonView>
    with
        StatefulPresenter<ContactDetailPersonView,
            BusinessContactDetailProvider, ContactDetailPersonProvider>,
        KeepAliveMixin {
  @override
  Widget buildWidget(BuildContext context) => ListView(
        key: PageStorageKey('Tab1'),
        physics: const ClampingScrollPhysics(),
        children: [
          AppSpaces.h20,
          SearchInputField(
            controller: provider.searchController,
            focusNode: provider.searchFocusNode,
          ),
          AppSpaces.h20,
          AppSpaces.grayH05,
          listBuilder<String>(
            selector: (_, __) => __.searchController.text,
            builder: (BuildContext context, String value, Widget child) =>
                Column(
              children: buildSearchWidget(value),
            ),
          )
        ],
      );

  List<Widget> buildSearchWidget(String value) {
    List<Widget> slivers;
    if (provider.searchController.text.isNotEmpty) {
      slivers = provider.data
          .toList()
          .where(_onSearchInputGroup)
          .map((e) => buildSliverItem(e))
          .toList();
    } else {
      slivers = provider.data.toList().map(buildSliverItem).toList();
    }
    return slivers;
  }

  Widget buildSliverItem(ContactPersonEntity data) {
    return listBuilder<ContactPersonEntity>(
      selector: (_, __) => data,
      builder:
          (BuildContext context, ContactPersonEntity value, Widget child) =>
              PressedOpacityButton(
        onPressed: () => provider.onNavContactPerson(value),
        child: ContactPersonItem(
          title: value.name,
          content: value.email,
          phoneNumber: value.phoneNumber,
          position: value.position,
        ),
      ),
    );
  }

  bool _onSearchInputGroup(ContactPersonEntity element) {
    String searchInput = provider.searchController.text;
    return RegExpUtils.getSearchRegExp(searchInput).hasMatch(element.name);
  }
}
